package fi.helsinki.cs.ohtu10k.view;

import javax.swing.*;
import java.awt.event.*;
import org.joda.time.DateTime;

import fi.helsinki.cs.ohtu10k.schedule.*;

/**
 * Class implementing main application window.
 * @author Simo Hellsten 
 * @author Hannu Kärnä
 * @author Thomas Nyman
 *
 */
public class ApplicationWindow implements Runnable {
	
    JFrame mainFrame;
    JMenuBar menuBar;
    JMenu menu, menu2;
    JMenuItem menuItem[];
    private WeekPanel weekView;
    private boolean scheduleSaved = true;
    
    static int WINDOW_WIDTH = 640;
    static int WINDOW_HEIGHT = 480;
    static int DAY_WIDTH = 80;
    static int HOUR_HEIGHT = 25;
    static int LABEL_WIDTH = 40;
    static int LABEL_HEIGHT = 40;
    static int HOUR_COUNT = 12;
    static int STARTING_HOUR = 8;
    static int OFFSETX_PX = 10;
    static int OFFSETY_PX = 10;
    
    public void run() {
        mainFrame = new JFrame("Opiskelijan aikataulun hallinta");
        mainFrame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
        mainFrame.addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent e) {
                exitApplication();
            }
        });
        
        // Attempt to automatically load saved schedule
        try {
        	ScheduleManagerHandler.loadSchedule("schedule.dat");
        } catch (Exception e) {}
        
        menuBar = new JMenuBar();
        menuItem = new JMenuItem[8];
        menu = new JMenu("Tiedosto");
        menuItem[0] = new JMenuItem("Uusi kalenteri");
        menuItem[1] = new JMenuItem("Lataa kalenteri");
        menuItem[2] = new JMenuItem("Tallenna kalenteri");
        menuItem[3] = new JMenuItem("Lopeta");
        menu2 = new JMenu("Muokkaa");
        menuItem[4] = new JMenuItem("Lisää kurssi");
        menuItem[5] = new JMenuItem("Lisää tapahtuma");
        menuItem[6] = new JMenuItem("Poista kurssi");
        menuItem[7] = new JMenuItem("Poista tapahtuma");
            
        menuBar.add(menu);
        menu.add(menuItem[0]);
        menu.add(menuItem[1]);
        menu.add(menuItem[2]);
        menu.add(menuItem[3]);
                    
        menuBar.add(menu2);
        menu2.add(menuItem[4]);
        menu2.add(menuItem[5]);
        menu2.add(menuItem[6]);
        menu2.add(menuItem[7]);
            
        mainFrame.setJMenuBar(menuBar);
            
        menuItem[5].addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e){
            	AddEventDialog.showNewEventDialog(mainFrame);
            	DateTime temp = weekView.getActiveDate();
	            mainFrame.remove(weekView);
	            weekView = new WeekPanel(DAY_WIDTH, HOUR_HEIGHT, LABEL_WIDTH, LABEL_HEIGHT, HOUR_COUNT, STARTING_HOUR, OFFSETX_PX, OFFSETY_PX);
	            weekView.setActiveDate(temp);
	            mainFrame.add(weekView);
	            mainFrame.validate();
	            scheduleSaved = false;
            }
        });
        
        menuItem[0].addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e){
            	ScheduleManagerHandler.resetScheduleManager();
            	DateTime temp = weekView.getActiveDate();
            	mainFrame.remove(weekView);
	            weekView = new WeekPanel(DAY_WIDTH, HOUR_HEIGHT, LABEL_WIDTH, LABEL_HEIGHT, HOUR_COUNT, STARTING_HOUR, OFFSETX_PX, OFFSETY_PX);
	            weekView.setActiveDate(temp);
	            mainFrame.add(weekView);
	            mainFrame.validate();
	            scheduleSaved = false;
            }
        });
        
        menuItem[7].addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e){
            	DateTime startDate = weekView.getActiveWeekFirstDate();             	
            	DeleteEventDialog.showDeleteEventDialog(mainFrame, startDate, startDate.plusWeeks(1));
            	DateTime temp = weekView.getActiveDate();
            	mainFrame.remove(weekView);
            	weekView = new WeekPanel(DAY_WIDTH, HOUR_HEIGHT, LABEL_WIDTH, LABEL_HEIGHT, HOUR_COUNT, STARTING_HOUR, OFFSETX_PX, OFFSETY_PX);
            	weekView.setActiveDate(temp);
            	mainFrame.add(weekView);
            	mainFrame.validate();
            	scheduleSaved = false;
            }
        });
        
        menuItem[1].addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e){
            	loadSchedule();
            	DateTime temp = weekView.getActiveDate();
            	mainFrame.remove(weekView);
	            weekView = new WeekPanel(DAY_WIDTH, HOUR_HEIGHT, LABEL_WIDTH, LABEL_HEIGHT, HOUR_COUNT, STARTING_HOUR, OFFSETX_PX, OFFSETY_PX);
	            weekView.setActiveDate(temp);
	            mainFrame.add(weekView);
	            mainFrame.validate();
            }
        });
        
        
        menuItem[2].addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e){
            	saveSchedule();
            }
        });
         
        menuItem[3].addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e){
            	exitApplication();
            }
        });
        
        menuItem[4].addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e){
            	AddCourseDialog.showAddCourseDialog(mainFrame);
            	scheduleSaved = false;
            }
        });
        
        menuItem[6].addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e){
            	DeleteCourseDialog.showDeleteCourseDialog(mainFrame);
            	scheduleSaved = false;
            	DateTime temp = weekView.getActiveDate();
            	mainFrame.remove(weekView);
	            weekView = new WeekPanel(DAY_WIDTH, HOUR_HEIGHT, LABEL_WIDTH, LABEL_HEIGHT, HOUR_COUNT, STARTING_HOUR, OFFSETX_PX, OFFSETY_PX);
	            weekView.setActiveDate(temp);
	            mainFrame.add(weekView);
	            mainFrame.validate();
            }
        });
        
        weekView = new WeekPanel(DAY_WIDTH, HOUR_HEIGHT, LABEL_WIDTH, LABEL_HEIGHT, HOUR_COUNT, STARTING_HOUR, OFFSETX_PX, OFFSETY_PX);
        mainFrame.add(weekView);
        mainFrame.setSize(WINDOW_WIDTH, WINDOW_HEIGHT);
        mainFrame.setVisible(true);
        mainFrame.validate();
    }
    
    public void exitApplication(){
    	if(!scheduleSaved) if(JOptionPane.showConfirmDialog(mainFrame, "Muutoksia ei ole talletettu.\nHaluatko tallettaa kalenterin?",
    	"Varoitus", JOptionPane.YES_NO_OPTION, JOptionPane.WARNING_MESSAGE)==JOptionPane.YES_OPTION) saveSchedule();
    	mainFrame.setVisible(false);
    	System.exit(0);
    }

    public void saveSchedule(){
    	
    	try{
    		ScheduleManagerHandler.saveSchedule("schedule.dat");
    	}
    	catch(Exception e){
    		JOptionPane.showMessageDialog(mainFrame, "Kalenterin talletus ei onnistunut.");
    		return;
    	}
    	scheduleSaved = true;
    	JOptionPane.showMessageDialog(mainFrame, "Kalenteri talletettu");
    }
    
    public void loadSchedule(){
    	
    	try{
    		ScheduleManagerHandler.loadSchedule("schedule.dat");
    	}
    	catch(Exception e){
    		JOptionPane.showMessageDialog(mainFrame, "Kalenterin lataus ei onnistunut.");
    		return;
    	}
    	scheduleSaved = true;
    	JOptionPane.showMessageDialog(mainFrame, "Kalenteri ladattu");
    }
    
    public static void main(String[] args) {
        Runnable app = new ApplicationWindow();
        try {
            SwingUtilities.invokeAndWait(app);
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }
}